﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CheccCoverplateCulvertAssemble.Common
{
    internal class ModelNameMerge
    {
        public List<string> MergeName(string dgnFileName)
        {
            if (dgnFileName==null)
            {
                return null;
            }
            
            List<ModelNameData> result = new List<ModelNameData>();
            List<string> modelNameList = GetDgnFileModels(dgnFileName);
            for (int i = 0; i < modelNameList.Count; i++)
            {
                //拆分
                string[] split = modelNameList[i].Split('_');
                string IndexText = split[2];
                string modelName = "";
                for (int l = 0; l < split.Count(); l++)
                {
                    if (l == 0)
                        modelName += split[l];
                    else if (l == 2)
                        continue;
                    else
                        modelName += "_" + split[l];
                }

                if (result.Count == 0)
                {
                    List<double> lengthOfPanel = new List<double>();
                    lengthOfPanel.Add(Convert.ToDouble(IndexText));
                    ModelNameData modelNameData = new ModelNameData();
                    modelNameData.Name = modelNameList[i];
                    modelNameData.lengthOfPanel = lengthOfPanel;
                    result.Add(modelNameData);
                }
                else
                {
                    bool boo = false;
                    for (int k = 0; k < result.Count; k++)
                    {
                        string[] resultSplit = result[k].Name.Split('_');
                        string resultModelName = "";
                        for (int l = 0; l < resultSplit.Count(); l++)
                        {
                            if (l == 0)
                                resultModelName += resultSplit[l];
                            else if (l == 2)
                                continue;
                            else
                                resultModelName += "_" + resultSplit[l];
                        }
                        if (modelName == resultModelName)
                        {
                            //有相同
                            result[k].lengthOfPanel.Add(Convert.ToDouble(IndexText));
                            boo = true;
                            break;
                        }
                    }
                    //无相同
                    if (boo == false)
                    {
                        List<double> lengthOfPanel = new List<double>();
                        lengthOfPanel.Add(Convert.ToDouble(IndexText));
                        ModelNameData modelNameData = new ModelNameData();
                        modelNameData.Name = modelNameList[i];
                        modelNameData.lengthOfPanel = lengthOfPanel;
                        result.Add(modelNameData);
                    }
                }
            }
            List<string> resultList = new List<string>();
            foreach (ModelNameData item in result)
            {
                double maxDouble = item.lengthOfPanel.Max();
                double minDouble = item.lengthOfPanel.Min();
                //拆分
                string[] split = item.Name.Split('_');
                string newModelName = "";
                for (int i = 0; i < split.Count(); i++)
                {
                    if (i == 0)
                        newModelName += split[i];
                    else if (i == 2)
                        newModelName += "_" + minDouble + "_" + maxDouble;
                    else
                        newModelName += "_" + split[i];
                }
                resultList.Add(newModelName);
            }
            return resultList;
        }
        private List<string> GetDgnFileModels(string dgnFileName)
        {
            string dgnFilePath = dgnFileName;
            DgnDocument document = DgnDocument.CreateForLocalFile(dgnFilePath);
            DgnFileOwner owner = DgnFile.Create(document, DgnFileOpenMode.ReadOnly);
            DgnFile cellFile = owner.DgnFile;
            cellFile.LoadDgnFile(out StatusInt fileLoadstatus);
            if (fileLoadstatus == StatusInt.Error)
            {
                return null;
            }
            var models = cellFile.GetModelIndexCollection();
            return models.ToList().Select(s => s.Name).Where(w => w.ToString() != "Default").ToList();
        }
    }
    class ModelNameData
    {
        public string Name { get; set; }
        public List<double> lengthOfPanel { get; set; }
    }
}

